<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<script type="text/javascript">
     $(function () {
        /**
         * 选择检验项目
         */
        $('#labType').change(function () {
            $("#performedByName").val(); // 清空科室
            $("#performedBy").val();
            $("#specimen").val();
            $("#labItemStr").html();// 清空检验子项目
            $("#clinicItemIds").val();

            var labTypeId = $(this).children('option:selected').val();
            if(labTypeId == '' || labTypeId == null ){
                return false;
            }
             labSubSelect(labTypeId,'');//查询其检验项目动态拼接
        });
        $("#btnSubmit").on("click",function(){
            var clinicItemIds=$("#clinicItemIds").val();
            if(clinicItemIds==null || clinicItemIds==""){
                toastr.error("请选择诊疗项目！");
            }else{
                $("#inputForm").submit();
                document.getElementById("inputForm").reset();
                $("#latItemStr").html("");
                $("#clinicItemIds").val("");
            }
          })
       })


     function updateItem(){
         var parentId=$("#labType").val();
         if(parentId==null || parentId==''){
             toastr.info("请选择检查项目类别后，在添加项目！");
             return false;
         }
         var clinicItemIds=$("#clinicItemIds").val();
         labSubSelect(parentId,clinicItemIds);
     }
     /**
      * 查询其检验项目动态拼接
      * @param labTypeId  检验项目Id
      * @param clinicItemIds  诊疗项目Ids
      */
     function labSubSelect(labTypeId,clinicItemIds){
         var dragHtml ='<div id="labDivId" style="overflow-y:auto;"   class="drag-main">';
         dragHtml+='		<dl id="labSelected" class="drag-selected"><dt>已选项目：</dt><dd class="drag-main-div"></dd></dl>';
         dragHtml+='</div>';
         $('#drag_h').html('<b>请选择检验项目</b><span onclick="exam.confirm()">确定</span>');
         $('#drag_con').html(dragHtml);
         exam.Show(labTypeId,clinicItemIds);
         boxAlpha();
         draglayer();
         $("#labDivId").height($("#drag").height()-65);
     }
     var exam = {
         // 请选择地区
         init : function(clinicItemIds,divId){
             var strs= new Array(); //定义一数组
             strs=clinicItemIds.split(","); //字符分割
             for (var i=0;i<strs.length ;i++ )
             {
                 if($("#labSelected dd").find("#"+strs[i]).length==0){
                     exam.Chk(''+strs[i]+'',divId);
                 }
             }
         },
         Show : function(labTypeId,clinicItemIds){
             $.ajax({
                 type: "POST",
                 url: '${ctx}/lab/labApply/queryBylabTypeId?id=' + labTypeId,
                 dataType: "json",
                 success: function (data) {
                     if (data.length > 0) {
                         for(var i=0;i<data.length;i++){
                             var examHtml="";
                                 examHtml+='<div id="maincity'+i+'">';
//                                 examHtml+='<h4>'+data[i].name+':</h4>';
                                 examHtml+='<dd class="drag-main-div">';
                             for(var j=0;j<data[i].clinicItemDictList.length;j++) {
                                 examHtml+='<li class="selected"  price="'+data[i].clinicItemDictList[j].clinicItemDict.price+'" id="'+data[i].clinicItemDictList[j].clinicItemDict.id+'"   performedBy="'+data[i].clinicItemDictList[j].orgDept.id+'"specimen="'+data[i].clinicItemDictList[j].specimen+'"specimenName="'+data[i].clinicItemDictList[j].specimenName+'" deptName="'+data[i].clinicItemDictList[j].orgDept.deptName+'" onclick="exam.Chk(\''+data[i].clinicItemDictList[j].clinicItemDict.id+'\',\'maincity'+i+'\')">'+data[i].clinicItemDictList[j].clinicItemDict.itemName+'(标本:'+data[i].clinicItemDictList[j].specimenName+')</li>';
                             }
                             examHtml+='</dd></dl>';
                             examHtml+='</div>';
                             $("#labSelected").after(examHtml);
                             if(clinicItemIds!="" && clinicItemIds!=null){
                                 exam.init(clinicItemIds,'maincity'+i+'');
                             }
                         }
                     }
                 }
             });
         },
         Chk: function(id,divId){
//             $("#"+id).addClass("chkON");
             var isChk= $("#"+id).attr("class");
             if(isChk.indexOf("chkON") != -1){
                 $("#chk"+id).remove();
                 $("#"+id).attr("class","selected");
                 return false;
             }
             $("#"+id).addClass("chkON");
             var chkHtml=$("#"+id).clone(true);
             var liXm=$(chkHtml).attr("id","chk"+id);
             $("#labSelected dd").append(liXm);
//             $("#"+id).attr("onclick","exam.del('"+id+"','"+divId+"')");
//             $("#labSelected dd").append($("#"+id));
             $("#chk"+id).attr("onclick","exam.del('chk"+id+"')");
             $("#chk"+id).attr("chkId",id);

         },
         del: function(id,divId) {
//             $("#"+id).removeClass("chkON");
//             $("#"+id).attr("onclick","exam.Chk('"+id+"','"+divId+"')");
//             $("#"+divId+" dd").append($("#"+id));
             var chkId=$("#"+id).attr("chkId");
             $("#"+id).remove();
             $("#"+chkId).attr("class","selected");
         },
         confirm:function(){
             var itemIds="";
//             var deptId=$("#performedBy").val();
             var deptId=""; // 科室标记
             var specimenId = "";//标本标记
             var performedBy = "";
             var specimenIds = "";
             $("#labItemStr").html("");
             $("#labSelected dd li").each(function(index,domEle){
                 if(index == 0){
                     deptId = $(this).attr("performedBy");
                     specimenId = $(this).attr("specimen");
                 }
                 performedBy=$(this).attr("performedBy");
                 specimenIds=$(this).attr("specimen");
                 //确认拿到执行科室
                 if(deptId==null || deptId=="") {
                     toastr.error("获取执行科室失败！");
                     return false;
                 } else if(deptId!=performedBy) {
                     toastr.error("与已选择的诊疗项目不是同一执行科室！");
                     return false;
                 } else if(specimenId!=specimenIds) {
                     toastr.error("两个项目不是同一标本！");
                     return false;
                 } else {
                     $("#performedBy").val(deptId);
                     itemIds+=$(this).attr("chkId")+",";
                     var item=$(this).html()+"￥:"+'<font color="red">'+$(this).attr("price")+'</font>';
                     var deptName=$(this).attr("deptName");
                     var specimen = $(this).attr("specimen");
                     var specimenName = $(this).attr("specimenname");
                     if(specimen =="undefined"){
                         toastr.error("这个项目没有检验标本！");
                         return false;
                     }
                     $("#performedByName").val(deptName);
                     $("#specimen").val(specimen);
                     $("#specimenName").val(specimenName);
                 }

                 $("#labItemStr").append(item+"<br>");
             });
             $("#clinicItemIds").val(itemIds.substring(0,itemIds.length-1));
             boxAlpha();
         }
     }


</script>
<div class="content-wrap no-p">
    <div class="wrapper">
        <%--填写数据--%>
        <div class="col-lg-11 mt10">
            <form:form id="inputForm" data-parsley-validate="" modelAttribute="labItem"
                       onsubmit="return formSaveLoad('rightDiv','inputForm','${ctx}/lab/labApply/save','${ctx}/lab/labApply/queryByCondition?visitId=${labApply.visitId}&patientId=${labApply.patientId}&inoroutFlag=1');"
                       method="post" class="form-horizontal">
                <form:hidden path="labApply.patientId" /><%--病人Id--%>
                <form:hidden path="labApply.visitId" /><%--病人住院Id--%>
                <form:hidden path="clinicItemIds"/><%--诊疗项目Ids已逗号分隔--%>
                <form:hidden path="itemName"/><%--检验项目名称--%>
                <form:hidden path="labApply.implementDept" id="performedBy"/><%--执行科室Id--%>
                <form:hidden path="labApply.inoroutFlag" value="1"/>
                <div class="form-group">
                    <label class="col-sm-3 control-label">检验类别：</label>
                    <div class="col-sm-3">
                        <form:select path="" id="labType"  data-parsley-required="true"  class="form-control" onchange="">
                            <form:option value="" label="请选择"></form:option>
                            <form:options items="${labTypeList}" itemLabel="name" itemValue="id" htmlEscape="false"/>
                        </form:select>
                    </div>
                    <label class="col-sm-3 control-label">检验科室：</label>
                    <div class="col-sm-3">
                        <form:input path="" id="performedByName" htmlEscape="false" class="form-control"
                                    disabled="true"/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">标本：</label>

                    <div class="col-sm-9">
                        <form:input path="" id="specimenName"  htmlEscape="false" class="form-control"
                                    disabled="true"/>
                        <input type="hidden" name="labApply.specimen" id="specimen">
                    </div>
                </div>
                <%--<div class="form-group">--%>
                    <%--<label class="col-sm-2 control-label">子类别：</label>--%>

                    <%--<div class="col-sm-10">--%>
                        <%--<form:select path="itemCode" id="childExamType" class="form-control">--%>
                            <%--<form:option value="" label="请选择"></form:option>--%>
                        <%--</form:select>--%>
                    <%--</div>--%>
                <%--</div>--%>
                <div class="form-group">
                    <label class="col-sm-3 control-label">诊疗项目</label>
                    <div class="col-sm-9">
                        <div class="item_b" style="min-height: 60px" id="labItemStr" onclick="updateItem()" data-parsley-required="true">

                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label">标本说明：</label>

                    <div class="col-sm-9">
                        <form:input path="labApply.specimenExplain" htmlEscape="false" class="form-control" maxlength="30"/>
                    </div>
                </div>
                 <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <input id="btnSubmit" class="btn btn-primary" type="button" value="保 存">&nbsp;
                    </div>
                </div>
            </form:form>
        </div>

    </div>
</div>
